package com.mycookbook;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EggTimerFragment extends Fragment
{
    private String mContent = "";
    private String text = "";

	private static final int NOTIF_ID = 1;
    
    private static final String KEY_CONTENT = "EggTimerFragment:Content";
    private static final String KEY_TEXT = "EggTimerFragment:Text";
    private static final String KEY_BUTTON = "EggTimerFragment:Button";
    
    private int startTimerButton;
    private Button startTimerButtonId;
    private int setTimer;
    private EditText setTimerId;
    private int inputValue;
    private EditText inputValueId;
    
	private static final int MY_NOTIFICATION_ID=1;
	private NotificationManager notificationManager;
	private Notification myNotification;
		
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Log.e("Test", "EggTimerFragment onCreate()");
        
        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT) && savedInstanceState.containsKey(KEY_TEXT))
        {
            mContent = savedInstanceState.getString(KEY_CONTENT);
            text = savedInstanceState.getString(KEY_TEXT);
            startTimerButton = savedInstanceState.getInt(KEY_BUTTON);              
        }
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        Log.i("Test", "ImageFragment onActivityCreated()");
        
        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT) && savedInstanceState.containsKey(KEY_TEXT))
        {
            mContent = savedInstanceState.getString(KEY_CONTENT);
            text = savedInstanceState.getString(KEY_TEXT);
            startTimerButton = savedInstanceState.getInt(KEY_BUTTON);
        }
    }
 
    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
        outState.putString(KEY_TEXT, text);
        outState.putInt(KEY_BUTTON, startTimerButton);
    }
        
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
    	View view = inflater.inflate(R.layout.eggtimerfragment, container, false);
       
        startTimerButtonId = (Button) view.findViewById(R.id.buttonStartTimer);
        setTimerId = (EditText) view.findViewById(R.id.timerValue);
        inputValueId = (EditText) view.findViewById(R.id.timerValue);
        
        startTimerButtonId.setOnClickListener(new OnClickListener()
        {
			public void onClick(View v)
			{
				 onClickStarTimer();
				 
			}
		});
        
        return view;
    }

    public static EggTimerFragment newInstance(int setTimer, int startTimerButton)
    {
    	EggTimerFragment fragment = new EggTimerFragment();
        fragment.setTimer = setTimer;
        fragment.startTimerButton = startTimerButton;

        return fragment;
    }

	public void onClickStarTimer()
	{
		// lav asynctask private class Eggtimer extends AsyncTask<Void, Void, Void>{
	   
		if(inputValueId.getText().toString().equals("") || inputValueId.getText().toString().equals("0"))
		{    		
			Toast.makeText(getActivity(), "Please input a value number", Toast.LENGTH_SHORT).show();
			return;
		}
	
		int seconds = 1000*Integer.parseInt(inputValueId.getText().toString());
	
		inputValueId.setFocusable(false);
		inputValueId.setClickable(false);
		startTimerButtonId.setEnabled(false);
		
		StartCountDownTimer(seconds);
		
		return;
	}

	private void StartCountDownTimer(int input)
	{
		new CountDownTimer(input, 1000)
		{
			public void onTick(long millisUntilFinished)
			{
				setTimerId.setText("" +  millisUntilFinished / 1000);
			}

			public void onFinish()
			{
				inputValueId.setFocusableInTouchMode(true);
				inputValueId.setClickable(true);
				startTimerButtonId.setEnabled(true);
				setTimerId.setText("0");
				
				DisplayNotification();
			}
		}.start();
	}
    
//	private void StartTimer()
//	{
//		String msg;    	
//		Intent startAlarmService = new Intent(getActivity(), Alarm.class);
//		msg = inputValueId.getText().toString();
//		startAlarmService.putExtra("1", msg);
//	
//		if(!msg.contentEquals(""))
//			getActivity().startService(startAlarmService);
//	}
	
	private void DisplayNotification()
	{
		String notificationText = "Food is ready!";
		String notificationTitle = "My Cookbook";
		
		notificationManager = (NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
		myNotification = new Notification(R.drawable.ic_launcher,"Food is ready!", System.currentTimeMillis());
		Context context = getActivity().getApplicationContext();
		   
		Intent myIntent = new Intent(getActivity(), MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, myIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
		
		myNotification.defaults |= Notification.DEFAULT_SOUND;
		myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
		myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
		notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
	}

}


    